<?php

class links_rating
{
	var $mydirname;
	var $controller_name;
	var $Links;
	var $db_model;

	/** Constructor
	 *
	**/
	function links_rating($mydirname)
	{
		$this->mydirname = $mydirname;
		$this->controller_name = str_replace('links_', '', get_class($this));
		$this->Links = links_Base::getInstance($this->mydirname);
		$this->Links->setControllerName($this->controller_name);
		$this->Links->breadcrumbs();
	}

	function index($lid = 0)
	{
		$this->form($lid);
	}

	function form($lid = 0)
	{
		if ($link = $this->Links->getDataByLid($lid)) {

			$bread = $this->Links->getBreadcrumbs();
			$bread[] = array('name' => $link['title']);
			$data = array(
				'link'              => $link,
				'xoops_breadcrumbs' => $bread,
				'gticket'           => $GLOBALS['xoopsGTicket']->getTicketHtml(__LINE__,1800,'links_rating'),
			);
			$template = $this->Links->getTemplatePrefix() . '_rating.html';
			$this->Links->view($template, $data);

		} else {
			$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, _MD_CANT_GET_DATA);
			exit();
		}
	}

	function post()
	{
		$js_check = $this->Links->is_mobile ? FALSE: TRUE;
		$this->Links->basicPostCheck('links_rating', $js_check);

		$lid = (isset($_POST['lid']) && !empty($_POST['lid'])) ? intval($_POST['lid']) : 0 ;

		$return_msg = $this->_post_common($lid);

		if (empty($return_msg)) {
			$this->Links->linksRedirectHeader($this->Links->makeUrl('index', 'item', array('lid' => $lid)), 2, _MD_THANKURATE);
		} else {
			$this->Links->linksRedirectHeader($this->Links->baseUrl(TRUE), 2, $return_msg);
		}

	}

	function _post_common($lid)
	{
		$msg = '';

		if ($link = $this->Links->getDataByLid($lid)) {
			$this->db_model = $this->Links->setDbModel($this->controller_name);

			//Check
			if ($this->Links->uid != 0) {
				//The submitter cannot vote.
				$this->Links->setWhere('lid', $lid, '=');
				$data = $this->Links->getDatas();
				foreach ($data as $d) {
					if ($d['submitter'] == $this->Links->uid) {
						$msg = _MD_CANTVOTEOWN;
						return $msg;
					}
				}
				//One man one vote
				$this->db_model->setWhere('lid', $lid, '=');
				$datas = $this->db_model->getDatas();
				foreach ($datas as $data) {
					if ($data['ratinguser'] == $this->Links->uid) {
						$msg = _MD_VOTEONCE2;
						return $msg;
					}
				}
			} else {
				$ip = xoops_getenv("REMOTE_ADDR");

				if ($this->Links->is_mobile) {
					if (empty($ip)) {
						$msg = 'Rating Error: '._MD_ANY_PROBLEM;
						return $msg;
					}
				}
				//It is from one IP only to one vote for the same link.
				$this->db_model->setWhere('lid', $lid, '=');
				$this->db_model->setWhere('ratinghostname', $ip, '=');
				$count = $this->db_model->getCount();
				if ($count > 0) {
					$msg = _MD_VOTEONCE2 ."($count)";
					return $msg;
				}
			}

			if ($this->db_model->insert($lid)) {
				$this->_update_rating($lid);
				return $msg;
			} else {
				$msg = _ERRORS;
				return $msg;
			}
		} else {
			$msg = _MD_CANT_GET_DATA;
			return $msg;
		}
	}

	function post_j()
	{
		$lid = (isset($_POST['lid']) && !empty($_POST['lid'])) ? intval($_POST['lid']) : 0 ;

		//Check Referer
		if (! (strpos(xoops_getenv('HTTP_REFERER'),XOOPS_URL)===0)) {
			die('Illegal Referer');
		}

		if (! $GLOBALS['xoopsGTicket']->check(TRUE, 'links_rating', FALSE)) {
			$new_gticket = $GLOBALS['xoopsGTicket']->issue(__LINE__, 1800, 'links_rating');
			echo $this->Links->returnEnc("[{'status':'ERR','lid':'". $lid ."','msg':'". str_replace("\n","",$GLOBALS['xoopsGTicket']->getErrors()) ."','mode':'rating','gticket':'". $new_gticket ."'}]");
			exit();
		}

		$return_msg = $this->_post_common($lid);

		$new_gticket = $GLOBALS['xoopsGTicket']->issue(__LINE__,1800,'links_rating');
		if (empty($return_msg)) {
			echo $this->Links->returnEnc("[{'status':'OK','lid':'". $lid ."','msg':'". _MD_THANKURATE ."','mode':'rating','gticket':'". $new_gticket ."'}]");
		} else {
			echo $this->Links->returnEnc("[{'status':'ERR','lid':'". $lid ."','msg':'". $return_msg ."','mode':'rating','gticket':'". $new_gticket ."'}]");
		}

	}

	function _update_rating($lid)
	{
		$this->db_model->setWhere('lid', $lid, '=');
		$votes_count = $this->db_model->getCount();

		if ($votes_count > 0) {
			$totalrating = 0;
			$this->db_model->setWhere('lid', $lid, '=');
			foreach ($this->db_model->getDatas() as $d) {
				$totalrating += $d['rating'];
			}

			$average_rating = $totalrating / $votes_count;
			$average_rating = number_format($average_rating, 4);

			return $this->Links->saveRating($lid, $average_rating, $votes_count);
		}
	}

}


